package geowiki.dataThread;

/* Daniel Shiffman               */
/* Programming from A to Z       */
/* Spring 2006                   */
/* http://www.shiffman.net       */
/* daniel.shiffman@nyu.edu       */

// A Simple Thread example
// Reads information from an XML feed every so often
// occuring to the "wait" variable

import geowiki.GeoLinkApp;
import geowiki.data.Link;
import java.util.ArrayList;
import java.util.List;

import geowiki.data.Place;
import geowiki.data.PlacemakerPlaceExtractor;
import geowiki.data.YQLLinkExtractor;
import geowiki.data.YQLQuery;
import geowiki.utils.WikipediaUtils;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import processing.core.PApplet;

public class CheckThread extends Thread {

	private PApplet p;
	private boolean running; // Is the thread running? Yes or no?
	private int wait; // How many milliseconds should we wait in between executions?
	private String article; // How many milliseconds should we wait in between executions?

	// Constructor, create the thread
	// It is not running by default
	public CheckThread(String article, PApplet p) {
		// wait = w;
		this.p = p;
		this.article = article;
		running = false;
	}

	// Overriding "start()"
	public void start() {
		// Set running equal to true
		running = true;
		// Print messages
		System.out.println("Starting thread (will execute every " + (wait / 1000) + " seconds.)");
		// Do whatever start does in Thread, don't forget this!
		super.start();
	}

	// We must implement run, this gets triggered by start()
	public void run() {
		while (running) {
			System.out.println("Checking.");
			check();
			// Ok, let's wait for however long we should wait
			try {
				sleep((long) (wait));
			} catch (Exception e) {
			}
		}
	}

	// Our method that quits the thread
	public void quit() {
		System.out.println("Quitting.");
		running = false; // Setting running to false ends the loop in run()
		// We used to need to call super.stop()
		// We don't any more since it is deprecated, see:
		// http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
		// super.stop();
		// Instead, we use interrupt, in case the thread is waiting. . .
		super.interrupt();
	}

	private void check() {

	}

	public List<List<Place>> loadLinkedPlaces(List<Link> links, String wikipediaArticleName) {

		links = loadLinks(wikipediaArticleName);
		List<List<Place>> linkedPlaces = new ArrayList<List<Place>>();

		for (int i = 0; i < links.size(); i++) {

			p.println(links.get(i) + " /" + i + " of " + links.size());
			String linkURL = WikipediaUtils.getFullURLFromLink(links.get(i).url);
			p.println("fullURL:" + linkURL);
			List<Place> placesOfLink = loadPlacesViaPlacemakerFromURL(linkURL);
			p.println("PlacesFound:" + placesOfLink);
			links.get(i).storePlaceMarkers(placesOfLink);
		}

		return linkedPlaces;
	}

	public List<Link> loadLinks(String wikipediaArticleName) {

		YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this.p);
		String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
		String restURL = YQLQuery.getAllLinksFromWebpage(wikipediaURL);
		List<Link> alllinks = linkExtractor.getLinks(restURL);
		return alllinks;

	}

	public List<Place> loadPlacesViaPlacemakerFromWiki(String wikipediaArticleName) {

		String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
		List<Place> allplaces = loadPlacesViaPlacemakerFromURL(wikipediaURL);

		return allplaces;
	}

	public List<Place> loadPlacesViaPlacemakerFromURL(String URL) {

		List<Place> allPlaces = new ArrayList<Place>();

		YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this.p);
		String[] paragraphs = linkExtractor.getMainParagraphs(URL);

		PlacemakerPlaceExtractor placeExtractor = new PlacemakerPlaceExtractor(this.p);
		for (int i = 0; i < paragraphs.length; i++) {
			PApplet.println("Getting places for " + i + ". p");
			List<Place> places = placeExtractor.getPlaces(paragraphs[i]);
			PApplet.println("\tplaces=" + places);
			allPlaces.addAll(places);
		}
		return allPlaces;
	}

}
